package db.tables;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hsqldb.Types;

import db.DBObject;

public class DBUserNameTable extends DBObject {
	public static final String TABLE_NAME = "user_name";

	public static final String USER_NAME_ID_COL = TABLE_NAME + "_id",
			USER_ID_COL = DBUserTable.USER_ID_COL,
			USER_NAME_COL = "user_name",
			START_DATE_COL = "start_date", END_DATE_COL = "end_date";

	public DBUserNameTable() throws ClassNotFoundException, SQLException {
		super(TABLE_NAME,
				new Object[][] {
						{ USER_NAME_ID_COL, Types.BIGINT,
							SQL_IDENTITY_PRIMARY_KEY, "" },
						{ USER_ID_COL, Types.BIGINT, SQL_NOT_NULL, "" },
						{ USER_NAME_COL, Types.VARCHAR, "unbenannt", "" },
						{ START_DATE_COL, Types.DATE, SQL_NOT_NULL, "" },
						{ END_DATE_COL,	Types.DATE, getSQLCheckEndDate()
								+getSQLForeignKeyReference(
										DBUserTable.TABLE_NAME, USER_ID_COL),
								"" } });
	}
	
	public String getCurrentUserName(Long user_id){
		String result=null;
		ResultSet rs=null;
		try {
			rs=selectOneColumnStatement(USER_NAME_COL, new String[]{USER_ID_COL,END_DATE_COL}, new Object[]{user_id,null});
			if(rs.next())
				result=rs.getString(USER_NAME_COL);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			closeResult(rs);
		}
		return result;
	}

	@Override
	protected void insertDefaultValues() {
	}
}
